<script>
import { onMounted, reactive, ref, toRefs } from "vue";
import { getsearchlist } from "../api/search";
import _ from "lodash";
import { useRouter } from "vue-router";
export default {
  emits: ["getkeyword"],
  setup(props, { emit }) {
    const input = ref(null);
    const router = useRouter();
    const list = reactive({
      textword: "",
      lists: "",
    });
    console.log(props);
    //输入改变
    let inputval = () => {
      // console.log(list.textword);
      gettext();
    };
    //获取keyword
    let getkeyword = () => {
      emit("getkeyword", list.textword);
      //本地存储
      let collection = window.localStorage.getItem("search");
      collection = JSON.parse(collection);
      if (collection.length > 0) {
        collection = collection.filter((item) => {
          return item != list.textword;
        });
      }

      collection.push(list.textword);

      collection = JSON.stringify(collection);
      window.localStorage.setItem("search", collection);
      //跳转
      router.push({
        name: "resultView",
      });
    };
    //列表获取keyword
    let gettexts = (text) => {
      emit("getkeyword", text);

      //本地存储
      let collection = window.localStorage.getItem("search");
      collection = JSON.parse(collection);
      if (collection.length > 0) {
        collection = collection.filter((item) => {
          return item != text;
        });
      }

      collection.push(text);

      collection = JSON.stringify(collection);
      window.localStorage.setItem("search", collection);

      router.push({
        name: "resultView",
      });
    };
    //请求
    let gettext = _.debounce(() => {
      getsearchlist({
        appKey: 4037465544,
        keyword: list.textword,
      }).then((data) => {
        list.lists = data.data;
        console.log("lists", list.lists);
      });
    }, 500);

    onMounted(() => {
      //聚焦
      input.value.focus();
      gettext();
    });

    return {
      input,
      inputval,
      gettext,
      getkeyword,
      gettexts,
      ...toRefs(list),
    };
  },
};
</script>
<template>
  <div class="searchView">
    <!-- 搜索框 -->
    <div class="search">
      <div class="left" @click="$router.go(-1)">
        <i class="iconfont icon-zuojiantou-copy"></i>
      </div>
      <div class="center">
        <input
          type="text"
          placeholder=""
          ref="input"
          @input="inputval"
          v-model="textword"
        />
        <i class="iconfont icon-sousuo"></i>
      </div>

      <!-- <router-link custom to="/resultView" v-slot="{ href, navigate }">
        <div :href="href" @click="navigate" class="right">搜索</div>
      </router-link> -->
      <div class="right" @click="getkeyword">搜索</div>
    </div>
    <van-empty description="请输入要查找的内容" v-if="!textword"/>

    <div class="items">
      <div
        class="item"
        v-for="item in lists"
        :key="item.tipWordId"
        v-show="lists.length > 0"
        @click="gettexts(item.tipWordName)"
      >
        <span class="iconfont">&#xe63b;</span>
        <span class="text">{{ item.tipWordName }}</span>
      </div>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.searchView {
  padding: 50px 0;
  .search {
    background-color: #fff;
    z-index: 10;
    position: fixed;
    border-bottom: 2px solid #f7f7f7;
    top: 0;
    left: 0;
    width: 100%;
    box-sizing: border-box;
    align-items: center;
    padding: 8px 15px;
    display: flex;
    justify-content: space-between;
    .center {
      width: 80%;
      position: relative;
      color: #b0b0b5;
      input {
        width: 100%;
        padding-left: 30px;
        box-sizing: border-box;
        font-size: 13px;
        border: none;
        background-color: #f6f6f6;
        border-radius: 8px;
        height: 30px;
      }
      i {
        position: absolute;
        left: 10px;
        top: 50%;
        font-size: 13px;
        transform: translateY(-50%);
      }
    }
  }
  .items {
    width: 100%;
    .item {
      width: 100%;
      height: 39px;
      box-sizing: border-box;
      padding: 0 15px;
      border-bottom: 1px solid rgb(247, 247, 247);
      line-height: 39px;
      font-size: 14px;
      color: rgb(28, 28, 28);
      .text {
        margin-left: 10px;
      }
    }
  }
}
</style>